1 using System
.ComponentModel
;
3 using UnityEngine
.Tilemaps
;
5 public class TintTextureGenerator
: MonoBehaviour
7 public int k_TintMapSize
= 256;
11 Refresh(GetComponent
<Grid
>());
14 private Texture2D m_TintTexture
;
15 private Texture2D tintTexture
19 if (m_TintTexture
== null)
21 m_TintTexture
= new Texture2D(k_TintMapSize
, k_TintMapSize
, TextureFormat
.ARGB32
, false);
22 m_TintTexture
.hideFlags
= HideFlags
.HideAndDontSave
;
23 m_TintTexture
.wrapMode
= TextureWrapMode
.Clamp
;
24 m_TintTexture
.filterMode
= FilterMode
.Bilinear
;
25 RefreshGlobalShaderValues();
31 public void Refresh(Grid grid
)
36 int w
= tintTexture
.width
;
37 int h
= tintTexture
.height
;
38 for (int y
= 0; y
< h
; y
++)
40 for (int x
= 0; x
< w
; x
++)
42 Vector3Int world
= TextureToWorld(new Vector3Int(x
, y
, 0));
43 tintTexture
.SetPixel(x
, y
, GetGridInformation(grid
).GetPositionProperty(world
, "Tint", Color
.white
));
49 public void Refresh(Grid grid
, Vector3Int position
)
54 RefreshGlobalShaderValues();
55 Vector3Int texPosition
= WorldToTexture(position
);
56 tintTexture
.SetPixel(texPosition
.x
, texPosition
.y
, GetGridInformation(grid
).GetPositionProperty(position
, "Tint", Color
.white
));
60 public Color
GetColor(Grid grid
, Vector3Int position
)
65 return GetGridInformation(grid
).GetPositionProperty(position
, "Tint", Color
.white
);
68 public void SetColor(Grid grid
, Vector3Int position
, Color color
)
73 GetGridInformation(grid
).SetPositionProperty(position
, "Tint", color
);
74 Refresh(grid
, position
);
77 Vector3Int
WorldToTexture(Vector3Int world
)
79 return new Vector3Int(world
.x
+ tintTexture
.width
/ 2, world
.y
+ tintTexture
.height
/ 2, 0);
82 Vector3Int
TextureToWorld(Vector3Int texpos
)
84 return new Vector3Int(texpos
.x
- tintTexture
.width
/ 2, texpos
.y
- tintTexture
.height
/ 2, 0);
87 GridInformation
GetGridInformation(Grid grid
)
89 GridInformation gridInformation
= grid
.GetComponent
<GridInformation
>();
91 if (gridInformation
== null)
92 gridInformation
= grid
.gameObject
.AddComponent
<GridInformation
>();
94 return gridInformation
;
97 void RefreshGlobalShaderValues()
99 Shader
.SetGlobalTexture("_TintMap", m_TintTexture
);
100 Shader
.SetGlobalFloat("_TintMapSize", k_TintMapSize
);